//
//  BiBorrowListViewController.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15-4-27.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiBorrowListViewController.h"
#import "BiCarBorrowEntity.h"
#import "BorrowListCell.h"
#import "MJRefresh.h"

#import "BiBorrowManageViewController.h"
@interface BiBorrowListViewController ()<UITableViewDataSource,UITableViewDelegate>{

UITableView * _tableView;

NSMutableArray * _borrowExecuteList;
}
@end

@implementation BiBorrowListViewController

- (id)init
{
    self = [super init];
    if (self)
    {
        _borrowExecuteList = [[NSMutableArray alloc] init];
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBackBtn];
    [self setNavBarTitle:@"借车审批列表"];
    [self setNavBarWithHidden:NO];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, [self viewTop], MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT-[self viewTop])];
    
    _tableView.delegate = self;
    
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    _tableView.rowHeight = 80;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setupRefresh];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:[BiUserEntity sharedInstance].userid,@"userId",
                              @"0",@"page",
                              @"10",@"pageSize",
                              @"2",@"status",
                              nil];
    [[BiNetEngine shareInstance] requestBorrowCarCheckList:self paraDict:paraDic];
    
    [self asyshowHUDView:WAITING CurrentView:self];
}

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    //    [_tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    _tableView.headerPullToRefreshText = @"下拉刷新";
    _tableView.headerReleaseToRefreshText = @"松开即可刷新";
    _tableView.headerRefreshingText = @"刷新中~";
    
    _tableView.footerPullToRefreshText = @"上拉加载更多数据";
    _tableView.footerReleaseToRefreshText = @"松开马上加载";
    _tableView.footerRefreshingText = @"加载中~";
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    NSLog(@"headerRereshing");
    
    [self performSelector:@selector(fffff) withObject:nil afterDelay:2];
}

- (void)fffff{
    
    [_tableView headerEndRefreshing];
    [_tableView footerEndRefreshing];
    
}

- (void)footerRereshing
{
    
    [self performSelector:@selector(fffff) withObject:nil afterDelay:2];
    NSLog(@"footerRereshing");
}

#pragma -mark UitableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    BiCarBorrowEntity * BorrowManageEntity = _borrowExecuteList[indexPath.row];
    BiBorrowManageViewController *BorrowManageViewController = [[BiBorrowManageViewController alloc] init];
//    传参
    BorrowManageViewController.borrowManageEntity = BorrowManageEntity;
    [self.navigationController pushViewController:BorrowManageViewController animated:YES];
    
}


#pragma -mark UitabeViewDatasource
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
            
            return _borrowExecuteList.count;
        
        
    }


//- (BorrowListCell *)cellForXingXiaoRowAtIndexPath:(NSIndexPath *)indexPath{
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * myIndentifier = @"BorrowListCell";

    
    BorrowListCell * cell = (BorrowListCell *)[_tableView dequeueReusableCellWithIdentifier:myIndentifier];
    
    if (!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BorrowListCell" owner:self options:nil] firstObject];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
//    // 定义一个静态变量做旗标，用于保证仅为该表格注册一次单元格视图
//    static BOOL isRegist = NO;
//    if(!isRegist)
//    {
//        // 加载*.xib界面设计文件
//        UINib* nib = [UINib nibWithNibName:@"BorrowListCell" bundle:nil];
//        // 注册单元格
//        [tableView registerNib:nib forCellReuseIdentifier:myIndentifier];
//        // 注册后将该旗标设为YES
//        isRegist = YES;
//    }
//    BorrowListCell* cell = [tableView
//                             dequeueReusableCellWithIdentifier:myIndentifier];
    
    // 从IndexPath参数中获取当前行的行号
//    NSUInteger rowNo = indexPath.row;
    
    
    
//    [cell setTiltleType:self.type];

        BiCarBorrowEntity * entity = [_borrowExecuteList objectAtIndex:indexPath.row];
        
        cell.labBorrowCarPerson.text = entity.borrowCarPersonName;
        
        cell.labVIN.text = entity.vin;
        
        cell.labApplyData.text = entity.applyDate;

    return cell;
    
}
#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    [self showMessage:entity_[@"result_info"]];
//    不等于200 返回
    if ([entity_[@"response_status"] integerValue] != 200) {
        
        return;
        
    }
    
    [_borrowExecuteList removeAllObjects];
    
    [_borrowExecuteList addObjectsFromArray:entity_[@"data"]];
    
    [_tableView reloadData];
    
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    [self removeHUDView:self];
}


@end
